package com.specificgroup.jira.plugin.acpro.action;

import com.specificgroup.jira.plugin.acpro.Constants;
import com.specificgroup.jira.plugin.acpro.exception.AcproException;
import com.specificgroup.jira.plugin.acpro.model.Settings;
import com.specificgroup.jira.plugin.acpro.model.acpro.AcproLoginData;
import com.specificgroup.jira.plugin.acpro.service.AcproManager;
import com.specificgroup.jira.plugin.acpro.session.SessionUtil;
import com.specificgroup.jira.plugin.acpro.xml.settings.generated.AcproLoginType;
import com.specificgroup.jira.plugin.acpro.xml.settings.generated.JiraAcproSettingsType;
import org.apache.log4j.Logger;

import java.io.IOException;

/**
 *
 * @author Ivan Holub
 */
public class AcproLoginAction extends AcproActionSupport {

    private static final String NEXT_ACTION = "AcproSelectAcproProject!default.jspa";

    private Logger log = Logger.getLogger(getClass());

    private AcproManager acproManager;

    private String acproPostUrl;

    private String acproUsername;

    private String acproPassword;

    public String getAcproPostUrl() {
        return acproPostUrl;
    }

    public void setAcproPostUrl(String acproPostUrl) {
        this.acproPostUrl = acproPostUrl;
    }

    public String getAcproUsername() {
        return acproUsername;
    }

    public void setAcproUsername(String acproUsername) {
        this.acproUsername = acproUsername;
    }

    public String getAcproPassword() {
        return acproPassword;
    }

    public void setAcproPassword(String acproPassword) {
        this.acproPassword = acproPassword;
    }

    public AcproLoginAction(AcproManager acproManager) {
        this.acproManager = acproManager;
    }

    public AcproManager getAcproManager() {
        return acproManager;
    }

    @Override
    public String doDefault() throws Exception {
        if (!hasPermissions()) {
            return PERMISSION_VIOLATION_RESULT;
        }

        Settings settings = SessionUtil.getSettings();
        JiraAcproSettingsType loadedSettings = settings.getLoadedSettings();
        if (loadedSettings != null) {
            AcproLoginType acproLoginType = loadedSettings.getAcproLogin();
            if (acproLoginType != null) {
                acproPostUrl = acproLoginType.getPostUrl();
                acproUsername = acproLoginType.getUserId();
            }
        }

        if (acproPostUrl == null) {
            acproPostUrl = Constants.ACPRO_POST_URL;
        }

        return super.doDefault();
    }

    @Override
    protected String doExecute() throws Exception {
        if (!hasPermissions()) {
            return PERMISSION_VIOLATION_RESULT;
        }
        
        AcproLoginData acproLoginData = new AcproLoginData();
        acproLoginData.setPostUrl(acproPostUrl);
        acproLoginData.setUsername(acproUsername);
        acproLoginData.setPassword(acproPassword);
        try {
            acproManager.acproLogin(acproLoginData);
        } catch (IOException e) {
            log.error("Error", e);
            addErrorMessage(e.toString());
            return ERROR;
        } catch (AcproException e) {
            log.error("Error", e);
            addErrorMessage(e.getMessage());
            return ERROR;
        }

        Settings settings = SessionUtil.getSettings();
        settings.setAcproLoginData(acproLoginData);

        String redirect = getRedirect(NEXT_ACTION);
        return redirect;
    }
}
